<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {

	public function News()
	{
		parent::__construct();
		$this->load->library('session');
		$this->load->model('admin_model','admin');
		$this->load->helper('functions');
	}

	public function index()
	{
		$data = $this->admin->getall('news');
		$this->load->view('admin/news_tpl',array('data'=>$data));
	}
	public function lists()
	{
		$data = $this->admin->getall('news');
		$i = 0;
		foreach($data as $d)
		{
			$data[$i]['CategoryName'] = $this->cagegoryname($d['CategoryID']);
		}
		echo json_encode($data);
	}

	public function add()
	{
		$category = '';
		if($_SERVER['REQUEST_METHOD']=='POST')
		{
			$title = $this->input->post('Title');
			$content = $this->input->post('Content');
			$author = $this->input->post('Autor');
			$from = $this->input->post('From');
			$time = $this->input->post('Inputtime') ? $this->input->post('Inputtime') : date("Y-m-d h:m:s",time());
			if($this->input->post('CategoryId4'))
			{
				$categoryid = $this->input->post('CategoryId4');
			}
			elseif($this->input->post('CategoryId3'))
			{
				$categoryid = $this->input->post('CategoryId3');
			}
			elseif($this->input->post('CategoryId2'))
			{
				$categoryid = $this->input->post('CategoryId2');
			}
			else
			{
				$categoryid = $this->input->post('CategoryId1');
			}
			$data = array(
				'Title'=>$title,
				'Content'=>$content,
				'Inputtime'=>$time,
				'Autor'=>$author,
				'From'=>$from,
				'CategoryID'=>$categoryid
			);
			$r = $this->admin->add('news',$data);
			if($r)
			{
				echo "发布成功";
			}
			else
			{
				echo "发布失败";
			}
		}
		else
		{
			$data = $this->admin->get_category();
			if($data)
			{
				foreach($data as $d)
				{
					$category .='<option value="'.$d['CategoryID'].'" >'.$d['CategoryName'].'</option>';
				}
			}

			$this->load->view('admin/news_from_tpl',array('actionurl'=>'index.php?c=news&m=add','categoryurl'=>'index.php?c=news&m=getcategory','category'=>$category));
		}
	}

	public function update()
	{
		if($_SERVER['REQUEST_METHOD']=='POST')
		{
			$id = $this->input->post('NewsId');
			$title = $this->input->post('Title');
			$content = $this->input->post('Content');
			$author = $this->input->post('Autor');
			$from = $this->input->post('From');
			$time = $this->input->post('Inputtime') ? $this->input->post('Inputtime') : date("Y-m-d h:m:s",time());
			if($this->input->post('CategoryId4'))
			{
				$categoryid = $this->input->post('CategoryId4');
			}
			elseif( $this->input->post('CategoryId3'))
			{
				$categoryid = $this->input->post('CategoryId3');
			}
			elseif($this->input->post('CategoryId2'))
			{
				$categoryid = $this->input->post('CategoryId2');
			}
			else
			{
				$categoryid = $this->input->post('CategoryId1');
			}
			$data = array(
				'Title'=>$title,
				'Content'=>$content,
				'Inputtime'=>$time,
				'Autor'=>$author,
				'From'=>$from,
				'CategoryID'=>$categoryid
			);
			$r = $this->admin->update('news',$data,array('NewsId'=>$id));
			if($r)
			{
				echo "修改成功";
			}
			else
			{
				echo "修改失败";
			}
		}
		else
		{
			$ids = $this->input->get('id');
			$category = '';
			$categorys = array();
			$result = $this->admin->getnews($ids);
			//print_r($result);exit;
			if($result['CategoryID'])
			{
				$pids = $this->admin->get_pcategory($result['CategoryID']);
				$pid = explode(',',$pids);
				$i = count($pid);
				foreach($pid as $p)
				{

					$data = $this->admin->get_category($p);
					$categorys[$i-1]='';
					if($i!=1) $categorys[$i-1] = '<select  onchange="category('.$i.',this.value)"  name="CategoryId'.$i.'"><option value="">请选择</option>';
					foreach($data as $d)
					{

						if($pid[$i-1] && $pid[$i-1]==$d['CategoryID'])
						{
							$categorys[$i-1] .='<option value="'.$d['CategoryID'].'" selected >'.$d['CategoryName'].'</option>';
						}
						elseif($d['CategoryID']==$result['CategoryID'])
						{
							$categorys[$i-1] .='<option value="'.$d['CategoryID'].'" selected >'.$d['CategoryName'].'</option>';
						}

						else
						{
							$categorys[$i-1] .='<option value="'.$d['CategoryID'].'"  >'.$d['CategoryName'].'</option>';
						}

					}
					if($i!=1) $categorys[$i-1].='</select>';
					$i--;
				}
			}
			$this->load->view('admin/news_from_tpl',array('actionurl'=>'index.php?c=news&m=update','categoryurl'=>'index.php?c=news&m=getcategory','category'=>$categorys[0],'categorys'=>$categorys,'data'=>$result));
		}
	}

	public function delete()
	{
		if($_SERVER['REQUEST_METHOD']=='POST')
		{
			$id = $this->input->post('ids');
			foreach($id as $i)
			{
				$r = $this->admin->delete('news',array('NewsId'=>$i));
			}
		}
		else
		{
			$id = $this->input->get('ids');
			$r = $this->admin->delete('news',array('NewsId'=>$id));
		}
		if($r)
		{
			echo "删除成功";
		}
		else
		{
			echo "删除失败";
		}
	}

	public function getcategory()
	{
		$string = '';
		$id = $this->input->get('id') ? $this->input->get('id') : 0;
		$data = $this->admin->get_category($id);
		if($data)
		{
			foreach($data as $d)
			{
				$string .='<option value="'.$d['CategoryID'].'" >'.$d['CategoryName'].'</option>';
			}

			echo $string;
		}

	}

	private function cagegoryname($id)
	{
		$data = $this->admin->get_category_info($id);
		if($data)
		{
			return $data['CategoryName'];
		}
		return false;
	}
}

/* End of file new_ctrl.php */
/* Location: ./application/controllers/new_ctrl.php */